package com.hsteach.service.patrol;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hsteach.common.core.domain.AjaxResult;
import com.hsteach.common.core.page.TableDataInfo;
import com.hsteach.common.exception.ServiceException;
import com.hsteach.common.utils.StringUtils;
import com.hsteach.dao.domain.patrol.Student;
import com.hsteach.dao.domain.patrol.Teacher;
import com.hsteach.dao.mapper.PatrolRoleMapper;
import com.hsteach.enums.BooleanEnum;
import com.hsteach.framework.feign.client.xgk.dto.GetTeacherInfoEntity;
import com.hsteach.framework.feign.client.xgk.dto.ret.*;
import com.hsteach.service.third.XgkService;
import com.hsteach.web.controller.patrol.dto.QueryStudentDTO;
import com.hsteach.web.controller.patrol.dto.StudentDTO;
import com.hsteach.web.controller.patrol.dto.TeacherDTO;
import com.hsteach.web.controller.patrol.vo.TeacherVO;
import com.hsteach.web.dto.ScoreRolePageDto;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *
 * </p>
 *
 * @author mgb
 * @version RoleService, java v1.0 2023-11-10 1:08 EXP $
 */
@Service
public class RoleService {

    @Resource
    public PatrolRoleMapper patrolRoleMapper;

    @Autowired
    public XgkService xgkService;

    public int addStudent(List<StudentDTO> studentDTOS) {
        List<Student> studentList = new ArrayList<>();
        for (StudentDTO studentDTO : studentDTOS) {
            Student student = new Student();
            BeanUtils.copyProperties(studentDTO, student);
            studentList.add(student);
        }
        StudentDTO studentDTO = studentDTOS.get(0);
        List<Student> students = patrolRoleMapper.queryStudentByNumber(studentDTOS,studentDTO.getOrgCode());
        if(!CollectionUtils.isEmpty(students)){
            AjaxResult.error("学生已存在，请勿重复新增！");
        }

        return patrolRoleMapper.addStudent(studentList);
    }

    public TableDataInfo<Student> queryStudentList(QueryStudentDTO student) {
        if (StringUtils.isEmpty(student.getOrgCode())){
            throw new ServiceException("orgCode不能为空");
        }
        PageHelper.startPage(student.getPageNum(), student.getPageSize());

        List<Student> students = patrolRoleMapper.queryStudentList(student);
        if (CollectionUtils.isEmpty(students)) {
            return null;
        }
        Long total =new PageInfo(students).getTotal();

        TableDataInfo<Student> tableDataInfo = new TableDataInfo(students, total);
        return tableDataInfo;
    }

    public int addTeacher(List<TeacherDTO> teacherDTOS) {
        List<Teacher> teachers = new ArrayList<>();
        for (TeacherDTO teacherDTO : teacherDTOS) {
            Teacher teacher = new Teacher();
            BeanUtils.copyProperties(teacherDTO, teacher);
            teacher.setClassHeader("JSBJZW_1".equals(teacher.getJobId()) ? BooleanEnum.TRUE.getCode() : BooleanEnum.FALSE.getCode());
            teacher.setTeacherId(teacherDTO.getTeacherId());
            teachers.add(teacher);
        }

        return patrolRoleMapper.addTeacher(teachers);
    }

    /**
     * 查询教师列表
     *
     * @param
     * @return
     */
    public TableDataInfo<TeacherVO> queryTeacherList(ScoreRolePageDto dto) {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        List<Teacher> teachers = patrolRoleMapper.queryTeacherList(dto);
        List<TeacherVO> teacherVOS = teachers.stream().map(teacher -> {
            TeacherVO teacherVO = new TeacherVO();
            teacherVO.setTeacherName(teacher.getTeacherName());
            teacherVO.setTeacherNumber(teacher.getTeacherNumber());
            teacherVO.setId(teacher.getId());
            teacherVO.setTeacherId(teacher.getTeacherId());
            return teacherVO;
        }).collect(Collectors.toList());
        Long total = new PageInfo(teachers).getTotal();
        TableDataInfo<TeacherVO> tableDataInfo = new TableDataInfo(teacherVOS, total);
        return tableDataInfo;
    }

    /**
     * 查询平台所有学生
     *
     * @param queryStudentDTO
     * @return
     */
    public List<Student> studentList(QueryStudentDTO queryStudentDTO) {

//        List<PersonInfoRet> persons =  xgkService.getStudentByClassIds();
        return null;
    }

    /**
     * @param classId
     * @return 1.通过班级号 获取班主任教师的编号
     * 2.通过教师编号，获取教师信息
     */
    public Teacher queryClassTeacher(String classId) {
        GetTeacherInfoEntity entity =new GetTeacherInfoEntity();
        entity.setClassId(classId);
        List<GetTeacherRet> teacherListByGradeAndClass = xgkService.getTeacherListByGradeInfo(entity);
        // 班主任 JSBJZW_1
        String teacherId = "";
        if (!CollectionUtils.isEmpty(teacherListByGradeAndClass)) {
            for (GetTeacherRet teacher : teacherListByGradeAndClass) {
                if (!CollectionUtils.isEmpty(teacher.getJobList())) {
                    List<JobList> jobLists = teacher.getJobList().stream()
                            .filter(jobList -> !StringUtils.isBlank(jobList.getJobId()))
                            .filter(jobList -> "JSBJZW_1".equals(jobList.getJobId()))
                            .collect(Collectors.toList());
                    if (!CollectionUtils.isEmpty(jobLists)) {
                        teacherId = teacher.getPersonId();
                    }
                }

            }
        }
        //通过教师id 去获取姓名
        List<GetTeacherRet> teachers = xgkService.getTeachers(teacherId);
        if(!CollectionUtils.isEmpty(teachers)){
           for (GetTeacherRet teacher : teachers) {
               if(teacherId.equals(teacher.getPersonId())){
                   Teacher teacherInfo = new Teacher();
                   teacherInfo.setTeacherNumber(teacherId);
                   teacherInfo.setTeacherName(teacher.getName());
                   return teacherInfo;
               }
           }
        }

        return  null;
    }

    public int deleteStudent(String studentId) {
        return patrolRoleMapper.deleteStudent(studentId);
    }

    public int deleteTeacher(String teacherId) {
        return patrolRoleMapper.deleteTeacher(teacherId);
    }
}
